package com.example.sikong.handler.mqtt;

import com.alibaba.fastjson.JSON;
import com.example.common.RedisKeyName;
import com.example.sikong.service.RedisProvider;
import com.example.util.SiKongMapDataUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

public class SiKongStateMqttMessageListener implements IMqttMessageListener {

    private final TypeReference<Map<String,Object>> mapTypeReference = new TypeReference<Map<String, Object>>() {};
    private final RedisProvider redisProvider;

    public SiKongStateMqttMessageListener(RedisProvider redisProvider) {
        this.redisProvider = redisProvider;
    }

    /**
     * {
     *           "OrganizationUUID":  "0c307af1-7056-440c-9455-f800e55ef59d",
     *           "TopicSn":  "4TADL2L001002T",
     *           "biz_code":  "device_state",
     *           "version":  "1.0",
     *           "timestamp":  1708681349411,
     *           "trace_id":  "",
     *           "data":  {
     *                     "host":  {
     *                               "live_capacity":  {
     *                                         "available_video_number":  3,
     *                                         "coexist_video_number_max":  3,
     *                                         "device_list":  [
     *                                                   {
     *                                                             "available_video_number":  1,
     *                                                             "camera_list":  [
     *                                                                       {
     *                                                                                 "available_video_number":  1,
     *                                                                                 "camera_index":  "165-0-7",
     *                                                                                 "coexist_video_number_max":  1,
     *                                                                                 "video_list":  [
     *                                                                                           {
     *                                                                                                     "switchable_video_types":  [
     *                                                                                                               "normal"
     *                                                                                                     ],
     *                                                                                                     "video_index":  "normal-0",
     *                                                                                                     "video_type":  "normal"
     *                                                                                           }
     *                                                                                 ]
     *                                                                       }
     *                                                             ],
     *                                                             "coexist_video_number_max":  1,
     *                                                             "sn":  "4TADL2L001002T"
     *                                                   },
     *                                                   {
     *                                                             "available_video_number":  2,
     *                                                             "camera_list":  [
     *                                                                       {
     *                                                                                 "available_video_number":  1,
     *                                                                                 "camera_index":  "39-0-7",
     *                                                                                 "coexist_video_number_max":  1,
     *                                                                                 "video_list":  [
     *                                                                                           {
     *                                                                                                     "switchable_video_types":  [
     *                                                                                                               "normal"
     *                                                                                                     ],
     *                                                                                                     "video_index":  "normal-0",
     *                                                                                                     "video_type":  "normal"
     *                                                                                           }
     *                                                                                 ]
     *                                                                       },
     *                                                                       {
     *                                                                                 "available_video_number":  1,
     *                                                                                 "camera_index":  "53-0-0",
     *                                                                                 "coexist_video_number_max":  1,
     *                                                                                 "video_list":  [
     *                                                                                           {
     *                                                                                                     "switchable_video_types":  [
     *                                                                                                               "normal",
     *                                                                                                               "wide",
     *                                                                                                               "zoom",
     *                                                                                                               "ir"
     *                                                                                                     ],
     *                                                                                                     "video_index":  "normal-0",
     *                                                                                                     "video_type":  "wide"
     *                                                                                           }
     *                                                                                 ]
     *                                                                       }
     *                                                             ],
     *                                                             "coexist_video_number_max":  2,
     *                                                             "sn":  "1581F5BMD232U001185X"
     *                                                   }
     *                                         ]
     *                               }
     *                     },
     *                     "sn":  "4TADL2L001002T",
     *                     "time_stamp":  1708681349411,
     *                     "timestamp":  1708681349411
     *           }
     * }
     * 目前只处理上报的直播摄像头相关数据
     * */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        byte[] payload = message.getPayload();
        String payloadStr = new String(payload);
        Map<String, Object> map = JSON.parseObject(payloadStr).getInnerMap();
        //  处理机库数据
        if (SiKongMapDataUtil.isLiveCapacity(map)) {
            doLiveCapacityStateData(map);
        }else if (SiKongMapDataUtil.isLiveStatus(map)) {
            doLiveStatusStateData(map);
        }
    }

    /**
     * 存储直播信息
     * */
    protected void doLiveCapacityStateData(Map<String,Object> map) {
        // 对应绑定无人机简要信息。
        List<Map<String,Object>> sub_device = SiKongMapDataUtil.getMapData("data.host.live_capacity.device_list", map);
        if (!CollectionUtils.isEmpty(sub_device)) {
            // 存储直播信息
            for (Map<String, Object> device : sub_device) {
                String sn = SiKongMapDataUtil.getMapData("sn", device);
                List<Map<String,Object>> cameraList = SiKongMapDataUtil.getMapData("camera_list", device);
                if (!CollectionUtils.isEmpty(cameraList)) {
                    redisProvider.set(RedisKeyName.SI_KONG_OSD_LIVE_CAPACITY_CAMERA_LIST+":"+sn,cameraList);
                }
            }
        }
    }


    /**
     * 存储直播信息
     * */
    protected void doLiveStatusStateData(Map<String,Object> map) {
        // 对应绑定无人机简要信息。
        List<Map<String,Object>> live_status = SiKongMapDataUtil.getMapData("data.host.live_status", map);
        if (!CollectionUtils.isEmpty(live_status)) {
            // 存储直播信息
            for (Map<String, Object> live_stat : live_status) {
                String video_id = SiKongMapDataUtil.getMapData("video_id", live_stat);
                if (StringUtils.isNotEmpty(video_id)) {
                    String channel = video_id.substring(0, video_id.lastIndexOf("/")).replace("/", "_");
                    redisProvider.set(RedisKeyName.SI_KONG_LIVE_SUCCESS+":"+channel,live_stat);
                }
            }
        }
    }
}
